import * as API from '../../api/index'
import * as UserAPI from '../../api/userFun.js'

const state = {
    evaluationData: [], // 评论数据（书包评论）
    bagData: [],  //图书总数据
    bookItem: {}, // 详情图书单条数据
    bookCatalogData: [],  // 目录总数据
    bookCatalog: [], // 前10条数据
    // bookCtPath:''  //详情页转跳
    subscriptionData: [], //订阅数据
    componentPath: '', //弹窗路径
    musicItem: {},//播放页面的数据
    subFlag: false, //获取订阅状态
    contact: [],
    scFlag: false,//收藏的一个状态
    scData: [],
    scsum: "",
    scsumData: [],//所有的收藏为true的存储
    musicIndex: 0,
    commentData: [], // 书本评论
    allShouData: [],  //收藏数据
    musici: "",//播放的集数传给弹框
    goodData: [], //点赞列表
    likeData: [],
    dowloadData:[],//下载的历史记录
    shoucangData:[],// 这里获取 收藏的全部
    querpath:"",//到搜索页面的path路径,
    musicBagItem:{},
    transmitList:[],  //播放列表
    transientMoney:0
}


function deWeight(data) {
    // console.log(data);
    data.forEach((item, index) => {
        let arr = []
        let spArr = item.typesname.split(",")
        spArr.forEach((item1, index1) => {
            if (arr.indexOf(item1) === -1) {
                arr.push(item1)
            }
        })
        item.typesname = arr
    })
    // console.log(arr);
    return data
}
const mutations = {  // 同步
    transientMoney(state,res){
        state.transientMoney=res
    },
    gaiMuserPlay(state,res){
        state.musicBagItem=res
    },
    clearMusicBag(state,res){
        state.musicBagItem={}
        // console.log(state.musicBagItem,'清空');
    },
    getData(state, res) {
        // res.info.forEach((item, index) => {
        //     let arr = []
        //     let spArr = item.typesname.split(",")
        //     spArr.forEach((item1, index1) => {
        //         if (arr.indexOf(item1) === -1) {
        //             arr.push(item1)
        //         }
        //     })
        //     item.typesname = arr
        // })
        console.log(res.info);
        state.bagData = res.info
    },
    getbookItem(state, item) {
        state.bookItem = item
    },
    // 目录
    getCatalog(state, res) {
        console.log(res);
        res.info.diversity.sort((a, b) => { return Number(a.diversityname) - Number(b.diversityname) })
        state.bookCatalogData = res.info.diversity
        state.bookItem = res.info.book[0]
       state.bookItem.typesname=state.bookItem.typesname.slice(0, 4)
        console.log(state.bookItem);
        state.bookCatalog = res.info.diversity.slice(0, 10)
        console.log(state.bookCatalog);

    },

    // 获取用户信息
    getUserMsg(state, item) {
        // state.bookUserMsg = item
        // console.log(item);
        state.shoucangData = item.collect
        state.dowloadData = item.download
        state.subscriptionData = deWeight(item.subscription)
        state.subFlag = state.subscriptionData.some((item, index) => {
            return item.bookid === state.bookItem.bookid;
        });

        state.evaluationData = item.evaluation   //  评论
        // console.log(state.shoucangData)
        // state.subscriptionData = item.subscription   // 订阅
        // state.subFlag = state.subscriptionData.some((item, index) => {
        //     return item.bookid === state.bookItem.bookid;
        // });
    },

    // 订阅数据同步方法
    subscriptionData(state, item) {
        // console.log(item,'dingyue');
        // state.subscriptionData = item.subscription
    },
    // 弹窗路径
    componentPath(state, path) {
        state.componentPath = path
    },
    //每个音频的数据
    getMusicItem(state, res) {
        // console.log(res.index,'33433');
        state.musicItem = res.item
        state.musicBagItem=state.bookItem
        console.log(state.musicBagItem,' state.musicBagItem');
        // state.musicBagList.push(state)
        // console.log(state.musicItem);
        state.musicIndex = res.index
    },

    // 获取书本评论
    getEvaluationData(state, res) {
        // console.log(res);
        if (res.info) {
            state.commentData = res.info.evaluation_record
            state.goodData = res.info.goodlist
            // console.log(state.commentData);
        } else {
            state.commentData = []
            state.goodData = []
        }
    },
    // 删除评论
    delCollect(state, res) {
        state.commentData = res.info

    },
    //收藏的音频
    getShouFlag(state, res) {
        state.scFlag = res.stFlag
        // console.log(res.stFlag,'传过来')
        //收藏的时候 
        if (res.stFlag === 'add') {
            if (state.scsumData.length == 0) {
                state.scsumData.push(res) //{stFlag: "add", sum: 2, bookid: "527c3f9f-0cb2-1a0e-ac8098be"}bookid: "527c3f9f-0cb2-1a0e-ac8098be"stFlag: "add"sum: 2__proto__: Object
            } else {
                //找到
                let i = state.scsumData.every(item => {
                    return item.sum === res.sum && item.bookid === res.bookid && item.userid === res.userid
                })
                // console.log(i,'我是i')
                if (!i) {
                    state.scsumData.push(res)
                }

            }
            // console.log(state.scsumData,'添加的时候找到的数组')  
        } else if (res.stFlag === 'del') {//取消收藏的时候
            let qi = state.scsumData.findIndex(item => {
                return item === res.sum && item.bookid === res.bookid && item.userid === res.userid
            })
            state.scsumData.splice(qi, 1)
        }

    },//总共的收藏的数据
    setCollect(state, res) {
        state.allShouData = res
    },//弹框的播放的集数
    setmusicIndex(state, res) {
        state.musici = res
    },
    addGood(state, res) {
        if (res.info) {

            state.likeData = res.info

        }
    },
    //存储到搜索页面的两个path路径
    queryPath(state,res){
         state.querpath = res
    }
}

const getters = {
    shoucangFang(state){
        let scIndex = state.scsumData.some(item=>{
            // console.log(item)
            // console.log(this.musicIndex+1,'index')
            return item.sum === state.musicIndex+1 && item.bookid===state.bookItem.bookid 
        })
        console.log(scIndex)
        return scIndex
    }
}


const actions = {   // 异步
    getBookData({ commit }, data) {
        return new Promise((resolve, reject) => {
            API.getBookList().then((res) => {
                commit('getData', res)
                resolve(res)
            })
        })
    },
    getCatalogData({ commit }, id) {
        // return await API.getCatalogData(id).then((res) => {
        //     commit('getCatalog', res)
        // })
        return new Promise((resolve, reject) => {
            API.getCatalogData(id).then((res) => {
                commit('getCatalog', res)
                resolve(res)
            })
        })
    },
    bookItem({ commit }, data) {
        commit('getbookItem', data)
    },

    // 获取用户数据
    // async getUserMsg({ commit }, id) {
    //     // console.log(id);
    //     return await UserAPI.getUserMsgData(id).then((res) => {
    //         // console.log(res, 'getusermsg');

    //     })
    // },
    getUserMsg({ commit }, id) {
        return new Promise((resolve, rej) => {
            UserAPI.getUserMsgData(id).then((res) => {
                // commit('getUserMsg', {
                //     subscription:res.subscription,
                //     evaluation:res.evaluation,
                //     download:res.download,

                // })
                console.log(res,'订阅');
                commit('getUserMsg', res)
                resolve(res)
            })
        })
    },

    // 订阅数据
    async subscription({ commit }, id) {
        // console.log(id);
        return await UserAPI.subscription(id).then((res) => {
            commit('subscriptionData', res)
        })
    },
    // 订阅书籍
    payBook({ commit }, id) {
        // console.log(id);
        return new Promise((resolve, rej) => {
            UserAPI.payBook(id).then((res)=>{
                resolve(res)
            })
        })
    },
    // 充值金额
    payMoney({ commit }, id) {
        // console.log(id);
        return new Promise((resolve, rej) => {
            UserAPI.payMoney(id).then((res) => {
                // commit('usersMsg',res)
                resolve(res)
            })
        })
    },
    // 关注
    contact({ commit }, data) {
        return new Promise((resolve, rej) => {
            UserAPI.contact(data).then((res) => {
                resolve(res)
            })
        })
    },
    // 取关
    delContact({ commit }, data) {
        return new Promise((resolve, rej) => {
            UserAPI.delContact(data).then((res) => {
                resolve(res)
            })
        })
    },
    // 添加评论
    addEvaluation({ commit }, data) {
        UserAPI.addEvaluation(data)
    },//用户收藏
    getCollect({ commit }, obj) {
        return new Promise((resolve, re) => {
            UserAPI.getCollect(obj).then(res => {
                commit('setCollect', res.info)
                resolve(res)

            })
        })

    },

    // 获取书本评论
    getEvaluation({ commit }, data) {
        return new Promise((resolve, rej) => {
            UserAPI.getEvaluation(data).then((res) => {
                commit('getEvaluationData', res)
                resolve(res)
            })
        })
    },
    // 书本点赞
    setGood({ commit }, data) {
        return new Promise((resolve, rej) => {
            UserAPI.setGood(data).then((res) => {
                commit('addGood', res)
                resolve(res)
            })
        })
    },

    // 删除书本评论
    delEvaluation({ commit }, data) {
        return new Promise((resolve, rej) => {
            UserAPI.delEvaluation(data).then((res) => {

                commit('delCollect', res)
                resolve(res)
            })
        })
    },
    //历史记录
    gethistory({ commit }, obj) {
        return new Promise((resolve, i) => {
            UserAPI.gethistory(obj).then((res) => {

                resolve(res)
            })
        })

    },
    downloads({ commit }, url) {
        return new Promise((resolve, i) => {
            UserAPI.downloads(url).then((res) => {

                resolve(res)
            })
        })
    },
    //下载记录
    getdownload({ commit }, obj) {
        return new Promise((resolve, i) => {
            UserAPI.download(obj).then((res) => {
                 console.log(res)
            })
        })
    }
}

export default {
    namespaced: true,
    state,
    getters,
    mutations,
    actions
}

